package co.com.smart.eagle.client.activity;


import co.com.smart.eagle.client.EagleCallBack;
import co.com.smart.eagle.client.factory.EagleFactory;
import co.com.smart.eagle.client.rpc.login.LoginRpc;
import co.com.smart.eagle.client.rpc.login.LoginRpcAsync;
import co.com.smart.eagle.client.util.session.EagleSession;
import co.com.smart.eagle.client.view.BarraBienvenidaView;
import co.com.smart.eagle.client.view.BarraBienvenidaView.Presenter;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

/**
 * 
 * @author Andres
 *
 */

public class BarraBienvenidaActivity  implements Presenter{

	private BarraBienvenidaView view;	
	private LoginRpcAsync loginRpc = GWT.create(LoginRpc.class); 

	public BarraBienvenidaActivity(EagleFactory eagleFactory){
		view = eagleFactory.getBarraBienvenidaView();	
		view.setPresenter(this);
	}

	public void start(AcceptsOneWidget panel) {
		panel.setWidget(view);
		consultarNombreUsuario();	
	}

	private void consultarNombreUsuario()
	{
		EagleCallBack<String> callBack = new EagleCallBack<String>(true) {

			@Override
			public void success(String userSession) {
				view.setNombreUsuario(userSession);
				EagleSession.getInstance().setUser(userSession);
			}
		};		
		loginRpc.getNombreUsuario(callBack);
	}

	@Override
	public void logOut() {
		
		EagleCallBack<Void> callBack = new EagleCallBack<Void>(true){

			@Override
			public void success(Void result) {
				EagleSession.getInstance().invalidateSession();
				Window.Location.reload();
			}
		};

		loginRpc.desconexion(callBack);
	}

}
